<?php

//服务器和数据库信息
$servername = "localhost";
$username = "root";
$password = "";
$dbName = "mydb";
$tableName = "testt";

//create connection
$conn = new mysqli($servername, $username, $password, $dbName);

//check connection 
if ($conn->connect_error) {
  # code...
  die('fail to connect' . $conn->connect_error);
}


// $sql = "SELECT COLUMN_NAME FROM information_schema.COLUMNS where TABLE_SCHEMA = $dbName and TABLE_NAME = $tableName";

// $result = $conn->query($sql);
// if ($result) {
//   echo($result . '2222');
// }else{

//   echo('3333');
// }

// data class
class insertData{

  protected $data = '';
  const varname = ['firstname', 'lastname', 'email'];
  var $sql = '';
  var $tableName = "";

  /**
   * 构造函数
   *
   * @param [要插入数据的表名] $tableName
   */
  function __construct($tableName)
  {
    $this->tableName = $tableName;
  }

  /**
   * 要插入的数据
   *
   * @param 姓 $a
   * @param 名 $b
   * @param 邮箱 $c
   */
  public function arrayData($a,$b,$c)
  {
    $firstname = $a;
    $lastname = $b;
    $email = $c;
    $this->data = "'" . implode("','",compact(static::varname))."'";
  }
  
  /**
   * 改变要插入的表格
   *
   * @param 表名 $tableName
   */
  public function changeTable($tableName)
  {
    $this->tableName = $tableName;
  }

  /**
   * mysqli要查询的字段
   */
  public function quertSql()
  {
    $this->sql .= "INSERT INTO $this->tableName (". implode(",",static::varname) .") VALUES ($this->data);";

  }
}

// 插入数据
$insertData = new insertData($tableName);

// echo($insertData->sql);

$insertData->arrayData('shima','wo',"test@example.com");
$insertData->quertSql();

$insertData->arrayData('ccc','wo',"tests@example.com");
$insertData->quertSql();

$insertData->arrayData('aaaa','woc',"tes1t@example.com");
$insertData->quertSql();

//插入数据
if ($conn->multi_query($insertData->sql) === true) {
  echo ("insert data success");
} else {
  echo ("fail to insert data" . $conn->error);
}

//关闭
$conn->close();